package com.maxd.service.UserCollectOrBrowse;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.*;
import com.maxd.respository.UserCollectOrBrowse.UserCollectOrBrowseRepository;
import com.maxd.respository.helpTask.HelpSendOrderRepository;
import com.maxd.respository.helpTask.HelpTakeOrderRepository;
import com.maxd.respository.helpTask.HelpTaskRepository;
import com.maxd.respository.timedTask.TimedTaskJpaRepository;
import com.maxd.respository.timedTask.TimedTaskRepository;
import com.maxd.respository.userinfo.UserRepository;
import com.maxd.service.helpTask.HelpUserScoreDetailsService;
import com.maxd.service.helpTask.HelpUserScoreService;
import com.maxd.service.message.IMessageService;
import com.maxd.service.money.IMoneyDetailsService;
import com.maxd.service.timedTask.TimedTaskService;
import com.maxd.service.userMoney.UserMoneyDetailsService;
import com.maxd.service.userinfo.UserServiceImpl;
import com.maxd.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;


@Service
public class UserCollectOrBrowseServiceImpl implements UserCollectOrBrowseService {

    @Autowired
    private UserCollectOrBrowseRepository userCollectOrBrowseRepository;
    private ReentrantReadWriteLock reentrantReadWriteLock=new ReentrantReadWriteLock(true);

    @Override
    public Result saveUserCollectOrBrowse(UserCollectOrBrowse userCollectOrBrowse){
        reentrantReadWriteLock.writeLock().lock();
        try{
            UserCollectOrBrowse userCollectOrBrowse1 = selectUserCollectOrBrowse(userCollectOrBrowse.getGoodsId(),userCollectOrBrowse.getClassify(), userCollectOrBrowse.getUserId());
            if(userCollectOrBrowse1!=null){
                userCollectOrBrowse1.setGoods(userCollectOrBrowse.getGoods());
                userCollectOrBrowseRepository.save(userCollectOrBrowse1);
            }else{
                userCollectOrBrowse.setCreateTime(DateUtil.createTime());
                userCollectOrBrowseRepository.save(userCollectOrBrowse);
            }
            return ResultUtil.success();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return ResultUtil.error(-1,"系统繁忙，请稍后再试！");
    }

    @Override
    public UserCollectOrBrowse selectUserCollectOrBrowse(String goodsId,Integer classify,Long userId){
        return userCollectOrBrowseRepository.selectUserCollectOrBrowse(goodsId,classify,userId);
    }

    @Override
    public Result deleteUserCollectOrBrowse(Long id){
        userCollectOrBrowseRepository.deleteById(id);
        return ResultUtil.success();
    }

    @Override
    public Result selectUserCollectOrBrowseList(Integer page,Integer size,Long userId,Integer classify,Integer goodsType){
        Pageable pageable = PageRequest.of(page, size, Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime")));
        // 构造自定义查询条件
        Specification<UserCollectOrBrowse> queryCondition = new Specification<UserCollectOrBrowse>() {
            @Override
            public Predicate toPredicate(Root<UserCollectOrBrowse> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (userId!=null) {
                    predicateList.add(criteriaBuilder.equal(root.get("userId"), userId));
                }
                if (classify!=null) {
                    predicateList.add(criteriaBuilder.equal(root.get("classify"), classify));
                }
                if (goodsType!=null) {
                    predicateList.add(criteriaBuilder.equal(root.get("goodsType"), goodsType));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return ResultUtil.success(userCollectOrBrowseRepository.findAll(queryCondition, pageable));
    }

}
